﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.Revit.MEP.Opening
{
	public partial class SteelDrivepipeSizeTableForm : System.Windows.Forms.Form
	{
		public SteelDrivepipeSizeTableForm()
		{
			this.InitializeComponent();
		}

		private void SteelDrivepipeSizeTableForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.InitDrivePipeDataByXML();
			this.dbGridSize.AllowUserToAddRows = false;
			this.UpdateDataGridView();
		}

		private void btnNew_Click(object sender, EventArgs e)
		{
			SteelDrivePipeData item = new SteelDrivePipeData();
			this.m_lstDrivePipeData.Add(item);
			this.UpdateDataGridView();
		}

		private void btnDel_Click(object sender, EventArgs e)
		{
			for (int i = this.dbGridSize.SelectedRows.Count; i > 0; i--)
			{
				DataGridViewRow dataGridViewRow = this.dbGridSize.SelectedRows[i - 1];
				this.m_lstDrivePipeData.Remove((SteelDrivePipeData)dataGridViewRow.Tag);
			}
			this.UpdateDataGridView();
		}

		private void dbGridSize_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			DataGridView dataGridView = (DataGridView)sender;
			if (dataGridView != null)
			{
				DataGridViewRow dataGridViewRow = dataGridView.Rows[e.RowIndex];
				SteelDrivePipeData steelDrivePipeData = (SteelDrivePipeData)dataGridViewRow.Tag;
				if (steelDrivePipeData != null)
				{
					switch (e.ColumnIndex)
					{
					case 1:
						steelDrivePipeData.MetalPipeDn = this.GetDataGridViewRowCellVal(dataGridViewRow, 1);
						return;
					case 2:
						steelDrivePipeData.PlasticPipeDn = this.GetDataGridViewRowCellVal(dataGridViewRow, 2);
						return;
					case 3:
						steelDrivePipeData.DrivePipeDn = this.GetDataGridViewRowCellVal(dataGridViewRow, 3);
						break;
					default:
						return;
					}
				}
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			List<DataGridViewRow> list = new List<DataGridViewRow>();
			int count = this.dbGridSize.Rows.Count;
			for (int i = 0; i < count - 1; i++)
			{
				DataGridViewRow dataGridViewRow = this.dbGridSize.Rows[i];
				dataGridViewRow.DefaultCellStyle.BackColor = this.m_colorValid;
				if (!this.IsValidDataGridViewRow(dataGridViewRow))
				{
					list.Add(dataGridViewRow);
				}
				for (int j = i + 1; j < count; j++)
				{
					DataGridViewRow dataGridViewRow2 = this.dbGridSize.Rows[j];
					dataGridViewRow2.DefaultCellStyle.BackColor = this.m_colorValid;
					if (!this.IsValidDataGridViewRow(dataGridViewRow2))
					{
						list.Add(dataGridViewRow2);
					}
					else if (this.IsDataGridViewRowEqualVal(dataGridViewRow, dataGridViewRow2) && !list.Contains(dataGridViewRow2))
					{
						list.Add(dataGridViewRow2);
					}
				}
			}
			if (list.Count > 0)
			{
				foreach (DataGridViewRow dataGridViewRow3 in list)
				{
					dataGridViewRow3.DefaultCellStyle.BackColor = this.m_colorInValid;
				}
				YJKMessageBox.Show("套管管径不能为空、或者存在一些重复的套管管径值, 请重新修正问题项!");
				base.DialogResult = DialogResult.None;
				return;
			}
			this.WriteSteelDrivePipeSizeConfigData();
		}

		private bool IsValidDataGridViewRow(DataGridViewRow row)
		{
			return !string.IsNullOrEmpty(this.GetDataGridViewRowCellVal(row, 3));
		}

		private bool IsDataGridViewRowEqualVal(DataGridViewRow rowThis, DataGridViewRow rowThat)
		{
			string dataGridViewRowCellVal = this.GetDataGridViewRowCellVal(rowThis, 3);
			string dataGridViewRowCellVal2 = this.GetDataGridViewRowCellVal(rowThat, 3);
			double num = this.ConvertString2Double(dataGridViewRowCellVal);
			return Math.Abs(this.ConvertString2Double(dataGridViewRowCellVal2) - num) <= 1E-06;
		}

		private double ConvertString2Double(string strVal)
		{
			double result = 0.0;
			try
			{
				result = Convert.ToDouble(strVal);
			}
			catch
			{
			}
			return result;
		}

		private string GetDataGridViewRowCellVal(DataGridViewRow row, int idx)
		{
			if (row != null)
			{
				DataGridViewTextBoxCell dataGridViewTextBoxCell = (DataGridViewTextBoxCell)row.Cells[idx];
				if (string.IsNullOrEmpty((string)dataGridViewTextBoxCell.Value))
				{
					return "";
				}
				string text = (string)dataGridViewTextBoxCell.Value;
				text = text.Trim();
				if (!string.IsNullOrEmpty(text))
				{
					return text;
				}
			}
			return "";
		}

		private void InitDrivePipeDataByXML()
		{
			this.m_lstDrivePipeData = ConfigDataOperate.ReadSteelDrivePipeSizeConfigData();
		}

		private void UpdateDataGridView()
		{
			this.dbGridSize.Rows.Clear();
			int num = 1;
			foreach (SteelDrivePipeData steelDrivePipeData in this.m_lstDrivePipeData)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				dataGridViewRow.Tag = steelDrivePipeData;
				HYGridViewNumbericCell hygridViewNumbericCell = new HYGridViewNumbericCell();
				DataGridViewCell dataGridViewCell = hygridViewNumbericCell;
				int num2 = num;
				num = num2 + 1;
				dataGridViewCell.Value = num2.ToString();
				dataGridViewRow.Cells.Add(hygridViewNumbericCell);
				HYGridViewNumbericCell hygridViewNumbericCell2 = new HYGridViewNumbericCell();
				hygridViewNumbericCell2.Value = steelDrivePipeData.MetalPipeDn;
				dataGridViewRow.Cells.Add(hygridViewNumbericCell2);
				HYGridViewNumbericCell hygridViewNumbericCell3 = new HYGridViewNumbericCell();
				hygridViewNumbericCell3.Value = steelDrivePipeData.PlasticPipeDn;
				dataGridViewRow.Cells.Add(hygridViewNumbericCell3);
				HYGridViewNumbericCell hygridViewNumbericCell4 = new HYGridViewNumbericCell();
				hygridViewNumbericCell4.Value = steelDrivePipeData.DrivePipeDn;
				dataGridViewRow.Cells.Add(hygridViewNumbericCell4);
				dataGridViewRow.DefaultCellStyle.BackColor = this.m_colorValid;
				this.dbGridSize.Rows.Add(dataGridViewRow);
			}
		}

		public bool WriteSteelDrivePipeSizeConfigData()
		{
			try
			{
				string filename = ConfigDataOperate.InitSteelDrivePipeSizeTableFile();
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.CreateXmlDeclaration("1.0", "utf-8", "yes");
				XmlNode xmlNode = xmlDocument.CreateElement("SteelDrivePipeSizeTable");
				foreach (object obj in ((IEnumerable)this.dbGridSize.Rows))
				{
					DataGridViewRow row = (DataGridViewRow)obj;
					XmlNode xmlNode2 = xmlDocument.CreateElement("SteelDrivePipeItem");
					XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("DrivePipeDn");
					xmlAttribute.Value = this.GetDataGridViewRowCellVal(row, 3);
					xmlNode2.Attributes.Append(xmlAttribute);
					XmlAttribute xmlAttribute2 = xmlDocument.CreateAttribute("MetalPipeDn");
					xmlAttribute2.Value = this.GetDataGridViewRowCellVal(row, 1);
					xmlNode2.Attributes.Append(xmlAttribute2);
					XmlAttribute xmlAttribute3 = xmlDocument.CreateAttribute("PlasticPipeDn");
					xmlAttribute3.Value = this.GetDataGridViewRowCellVal(row, 2);
					xmlNode2.Attributes.Append(xmlAttribute3);
					xmlNode.AppendChild(xmlNode2);
				}
				xmlDocument.AppendChild(xmlNode);
				xmlDocument.Save(filename);
				return true;
			}
			catch
			{
			}
			return false;
		}

		private List<SteelDrivePipeData> m_lstDrivePipeData;

		private System.Drawing.Color m_colorValid = System.Drawing.Color.LightYellow;

		private System.Drawing.Color m_colorInValid = System.Drawing.Color.Red;
	}
}
